package com.os.opencv.java.chapter5;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class WarpAffine {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //读取图像文件并在屏幕上显示
        Mat src = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/scl.jpg");

        //定义原图中三个点的坐标
        Point[] pt1 = new Point[3];
        pt1[0] = new Point(20, 56);
        pt1[1] = new Point(45, 160);
        pt1[2] = new Point(80, 150);

        //定义目标图像中3个点的坐标
        Point[] pt2 = new Point[3];
        pt2[0] = new Point(50, 80);
        pt2[1] = new Point(50, 180);
        pt2[2] = new Point(200, 180);

        //getAffineTransform()函数用到的数据类型
        MatOfPoint2f mop1 = new MatOfPoint2f(pt1);
        MatOfPoint2f mop2 = new MatOfPoint2f(pt2);

        //计算放射变换矩阵并进行仿射变换
        Mat dst = new Mat();
        Mat mat = Imgproc.getAffineTransform(mop1, mop2);
        Imgproc.warpAffine(src, dst, mat, src.size());

        //在原图像中标记3个点的坐标
        Scalar color = new Scalar(255, 255, 255);
        for(int n=0; n<3; n++){
            Imgproc.circle(src, pt1[n], 5, color, 2);
            Imgproc.putText(src, n+1+"", pt1[n], 0, 1, color, 3);
        }

        //在目标图像中标记3个点的坐标
        for(int n=0; n<3; n++){
            Imgproc.circle(dst, pt2[n], 5, color, 2);
            Imgproc.putText(dst, n+1+"", pt2[n], 0, 1, color, 3);
        }

        //在屏幕上显示变换前后的图像
        HighGui.imshow("src", src);
        HighGui.waitKey(0);
        HighGui.imshow("warped", dst);
        HighGui.waitKey(0);

        //在控制台输出仿射变换矩阵
        System.out.println(mat.dump());

        System.exit(0);
    }
}
